//
//  HStoreManager.swift
//  Drop
//
//  Created by hushaohua on 2017/11/16.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit
import CoreData

class HStoreManager: NSObject {
    
    static let defaultInstance = HStoreManager();
    class func defaultManger() -> HStoreManager{
        return defaultInstance;
    }
    
    lazy var persistentContainer:NSPersistentContainer = {
        let container = NSPersistentContainer(name:"Drop");
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)");
            }
        })
        return container;
    }();
    
    var context:NSManagedObjectContext{
        return self.persistentContainer.viewContext;
    }
    
    func saveContext() -> Void {
        if self.context.hasChanges{
            do{
                try context.save();
            }catch{
                let nserror = error as NSError
                fatalError("save context \(nserror), \(nserror.userInfo)")
            }
        }
    }
    
}
